package com.hlf.modules.merchant.store.controller;

import com.hlf.common.constants.Constants;
import com.hlf.common.constants.FileConstants;
import com.hlf.common.constants.SPDConstants;
import com.hlf.common.exception.BusinessException;
import com.hlf.common.utils.ReturnResult;
import com.hlf.modules.merchant.bank.entity.BankCard;
import com.hlf.modules.merchant.bank.service.BankCardService;
import com.hlf.modules.merchant.store.entity.Store;
import com.hlf.modules.merchant.store.entity.XdLInfo;
import com.hlf.modules.merchant.store.entity.XdLMerchantReportInfo;
import com.hlf.modules.merchant.store.service.StoreService;
import com.hlf.modules.merchant.store.service.XdLInfoExecuteEportService;
import com.hlf.modules.merchant.store.service.XdLInfoService;
import com.hlf.modules.sys.controller.AbstractController;
import com.hlf.modules.sys.entity.File;
import com.hlf.modules.sys.service.AreaService;
import com.hlf.modules.sys.service.FileService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 新大陆进件
 */
@RestController
@RequestMapping("/XDLInfo")
public class XDLSpdInfoController extends AbstractController {

    private static final Logger log = LoggerFactory.getLogger(XDLSpdInfoController.class);

    @Autowired
    BankCardService bankCardService;
    @Autowired
    private XdLInfoService xDLInfoService;
    @Autowired
    StoreService storeService;
    @Autowired
    FileService fileService;
    @Autowired
    XdLInfoExecuteEportService xdLInfoExecuteEportService;
    @Autowired
    AreaService areaService;

    /**
     * 新大陆进件接口
     *
     * @param xdlInfo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "inTo")
    public ReturnResult inTo(@RequestBody XdLMerchantReportInfo xdlInfo) throws Exception {
        Store store = storeService.queryObject(xdlInfo.getMerchantId());
        if (store == null) {
            throw new BusinessException("查询商户失败");
        }
        XdLInfo spd = xDLInfoService.selectByPrimaryKey(xdlInfo.getMerchantId());
        if (spd != null && spd.getMercId() != null && StringUtils.isNotBlank(spd.getMerchantInfo())
                && StringUtils.isNotBlank(spd.getMerchantImg())) {// 判断是否已经进件完成
            throw new BusinessException("已进件");
        }
        if (spd == null) {
            //商户进件
            spd = xDLInfoService.executeEport(xdlInfo);
        }
        //商户进件提交图片
        xdLInfoExecuteEportService.uploadImg(spd, xdlInfo);
        //商户提交
        xdLInfoExecuteEportService.merchantSubmit(spd);
        //记录商户进件标识
        xdLInfoExecuteEportService.executeEport(spd);

        return ReturnResult.ok();
    }

    /**
     * 新大陆信息修改接口
     *
     * @param xdlInfo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "update")
    public ReturnResult update(@RequestBody XdLMerchantReportInfo xdlInfo) throws Exception {
        Store store = storeService.queryObject(xdlInfo.getMerchantId());
        if (store == null) {
            throw new BusinessException("查询商户失败");
        }
        XdLInfo spd = xDLInfoService.selectByPrimaryKey(xdlInfo.getMerchantId());
        if (spd != null && spd.getMercId() != null && StringUtils.isNotBlank(spd.getMerchantInfo())
                && StringUtils.isNotBlank(spd.getMerchantImg())) {// 判断是否已经进件完成
            throw new BusinessException("已进件");
        }
        if (spd == null) {
            throw new BusinessException("商户未进件不能修改!");
        }
        xDLInfoService.updateEport(xdlInfo, spd);
        //商户进件提交图片
        xdLInfoExecuteEportService.uploadImg(spd, xdlInfo);
        //商户提交
        xdLInfoExecuteEportService.merchantSubmit(spd);
        //记录商户进件标识
        xdLInfoExecuteEportService.executeEport(spd);

        return ReturnResult.ok();
    }

    /**
     * 新大陆获取区域
     *
     * @throws Exception
     */
    @RequestMapping(value = "inToCode")
    public Map<String, Object> inToCode(String provNm, String cityNm) throws Exception {
        return xDLInfoService.findCodeNname(provNm, cityNm);
    }

    /**
     * 跳到进件页面
     */
    @RequestMapping(value = "/{id}/report")
    public ReturnResult toReport(@PathVariable("id") Long id) {
        Store store = storeService.queryObject(id);
        if (store == null) {
            throw new BusinessException("门店信息为空!");
        }

        String mobilePhone = store.getMobilePhone();

        XdLMerchantReportInfo merchantReportInfo = new XdLMerchantReportInfo();
        merchantReportInfo.setMerchantId(store.getId());
        merchantReportInfo.setStoeCntTel(mobilePhone);

        String di = areaService.selectByAreaCode(store.getProvince()).getName();
        String dc = areaService.selectByAreaCode(store.getCity()).getName();
        String duu = areaService.selectByAreaCode(store.getDistrict()).getName();

        // 商户名称(签购单名称)
        merchantReportInfo.setStoeNm(di + dc + store.getName());
        // 详细地址
        merchantReportInfo.setStoeAdds(di + dc + duu + store.getAddress());
        // 商户营业执照编号
        merchantReportInfo.setBusLicNo(store.getLicense());
        // 商户营业执照名
        merchantReportInfo.setBseLiceNm(store.getMerchantName());
        // 商户营业执照地址
        merchantReportInfo.setMercAdds(store.getAddress());
        // 商户营业执照有效期
        merchantReportInfo.setBusExpDt("");

        BankCard bankCard = bankCardService.selectByPrimaryKey(store.getProfitBankCardId());
        if (bankCard != null) {
            merchantReportInfo.setStlOac(bankCard.getCardNo());// 商户账户
            merchantReportInfo.setBnkAcnm(bankCard.getAccountName());// 开户账户户名
            merchantReportInfo.setCrpNm(store.getLegalperson());// 法人
            merchantReportInfo.setStoeCntNm(bankCard.getAccountName());// 法人
            merchantReportInfo.setCrpIdNo(bankCard.getIdCard());// 身份证号
            merchantReportInfo.setIcrpIdNo(bankCard.getIdCard());// 身份证号
            merchantReportInfo.setWcLbnkNo(bankCard.getCnapsCode());// 联行号
            Integer type = bankCard.getType();
            if (type != null) {
                if (type == 1) {
                    merchantReportInfo.setStlSign("1");
                } else if (type == 2) {
                    merchantReportInfo.setStlSign("0");
                }
            }
        }
        List<File> idCardList = fileService.select(FileConstants.IDENTITY_CARD_IMG, id);
        List<File> bankCardList = fileService.select(FileConstants.BANK_CARD_IMG, id);
        List<File> storeSignList = fileService.select(FileConstants.STORE_SIGN_IMG, id);
        List<File> openList = fileService.select(FileConstants.OPEN_ACCOUNT_LICENSE_IMG, id);
        List<File> businessImgList = fileService.select(FileConstants.BUSINESS_IMG, id);
        merchantReportInfo.setAuthenticationName(SPDConstants.MCH_NAME);
        merchantReportInfo.setIdentityCardImg(idCardList);
        merchantReportInfo.setBankCardImg(bankCardList);
        merchantReportInfo.setStoreSignImg(storeSignList);
        merchantReportInfo.setOpenLicenseImg(openList);
        merchantReportInfo.setBusinessImg(businessImgList);
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME, merchantReportInfo);

    }


    /**
     * 查询
     *
     * @throws Exception
     */
    @RequestMapping(value = "query")
    public ReturnResult inToCode(@RequestBody Long[] storeIds) throws Exception {
        for (Long storeId : storeIds) {
            XdLInfo xdLInfo = xDLInfoService.selectByPrimaryKey(storeId);
            if (xdLInfo != null) {
                xDLInfoService.getSaveStatus(xdLInfo);
            }
        }
        return ReturnResult.ok();

    }

}
